/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.baatar.opt.pga.impl.service;

import java.util.ArrayList;
import org.baatar.opt.pga.impl.client.PGAClusterConnection;
import org.baatar.opt.pga.intf.IPGACluster;
import org.baatar.remoting.service.RemotingServiceInfo;

/**
 *
 * @author Ba
 */
public class Cluster<T, M> {
    private int clusterIndex = 0;
    private RemotingServiceInfo clusterNetInfo = null;
    private IPGACluster<T, M> cluster = null;
    private ArrayList<Optimizer<M>> optimizers = null;

    public Cluster(RemotingServiceInfo coordinatorNetInfo, RemotingServiceInfo clusterNetInfo, int clusterIndex, int numOfWorkers) {
        this.clusterNetInfo = clusterNetInfo;
        this.clusterIndex = clusterIndex;
        PGAClusterConnection<T, M> clust = new PGAClusterConnection<T, M>();
        clust.setHostName(clusterNetInfo.getHostName());
        clust.setPortNo(clusterNetInfo.getListenerPort());
        clust.setCallerInfo(coordinatorNetInfo.toString());
        this.cluster = clust;
        this.optimizers = new ArrayList<Optimizer<M>>();

        for (int i = 0; i < numOfWorkers; i++) {
            this.optimizers.add(new Optimizer<M>(clusterIndex, i));
        }
    }

    public Cluster(IPGACluster<T, M> cluster, int clusterIndex, int numOfWorkers) {
        this.clusterIndex = clusterIndex;
        this.cluster = cluster;
        this.optimizers = new ArrayList<Optimizer<M>>();
        for (int i = 0; i < numOfWorkers; i++) {
            this.optimizers.add(new Optimizer<M>(clusterIndex, i));
        }
    }

    public void addConvergenceInfo(int optimizerIndex, int iteration, String convergenceInfo) {
        optimizers.get(optimizerIndex).addConvergenceInfo(iteration, convergenceInfo);
    }

    public StringBuilder getConvergenceInfo(int optimizerIndex) {
        return optimizers.get(optimizerIndex).getConvergenceInfo();
    }

//    public void setLastMigrant(int optimizerIndex, MigrantChromosome<M> migrant) {
//        optimizers.get(optimizerIndex).setLastMigrant(migrant);
//    }

    /**
     * @return the clusterNetInfo
     */
    public RemotingServiceInfo getClusterNetInfo() {
        return clusterNetInfo;
    }

    /**
     * @return the cluster
     */
    public IPGACluster<T, M> getCluster() {
        return cluster;
    }
}
